package com.dream.model.master;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.Type;

import com.dream.model.app.AppOrg;
import com.dream.model.base.AppUserModelBase;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

@Entity
@Table(name="t_mst_shop")

public class Shop extends AppUserModelBase implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6473742994319748024L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private String id;
	
	@Column(name="shop_type")
	@Enumerated(EnumType.STRING)
	private ShopType shopType;

	@Column(name="name")
	private String name;
	
	@Column(name="org_id")
	private Long orgId = 0L;
	
	@Column(name="address")
	private String address;
	
	@Column(name="contact")
	private String contact;
	
	@Column(name="shop_pic")
	private String shopPic;
	
	@Column(name="comments")
	private String comments;
	
	@Column(name="deleted")
	@Type(type = "org.hibernate.type.NumericBooleanType")
	private boolean deleted = false;
	
	@Transient
	private AppOrg firstOrg;
	
	@Transient
	private AppOrg secondOrg;
	
	public ShopType getShopType() {
		return shopType;
	}
	
	public void setShopType(ShopType shopType) {
		this.shopType = shopType;
	}

	public void initOrgs(List<AppOrg> li){
		
		if(CollectionUtils.isNotEmpty(li)){
			for(AppOrg org :li){
				if(org.getLevel().equals(1)){firstOrg = org;}
				if(org.getLevel().equals(2)){secondOrg = org;}
			}
		}
	}

	public AppOrg getFirstOrg() {
		return firstOrg;
	}

	public void setFirstOrg(AppOrg firstOrg) {
		this.firstOrg = firstOrg;
	}

	public AppOrg getSecondOrg() {
		return secondOrg;
	}


	public void setSecondOrg(AppOrg secondOrg) {
		this.secondOrg = secondOrg;
	}


	public String getShopPic() {
		return shopPic;
	}

	public void setShopPic(String shopPic) {
		this.shopPic = shopPic;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public Long getOrgId() {
		return orgId;
	}

	public void setOrgId(Long orgId) {
		this.orgId = orgId;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getContact() {
		return contact;
	}

	public void setContact(String contact) {
		this.contact = contact;
	}

	public String getComments() {
		return comments;
	}

	public void setComments(String comments) {
		this.comments = comments;
	}

	public boolean isDeleted() {
		return deleted;
	}

	public void setDeleted(boolean deleted) {
		this.deleted = deleted;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Shop other = (Shop) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
	public static enum ShopType{
		
	 shop,warehouse;
	 private static final Map<String,ShopType> map = Maps.newConcurrentMap();
	  private static final List<String> li = Lists.newArrayList();
	  static{
		  for(ShopType item :values()){
			  map.put(item.name(), item);
			  li.add(item.name());
		  }
	  }
	 
	  public static List<String> getTypes(){
		  return li;
	  }
	  
	  public static ShopType getInstance(String name){
		  if(StringUtils.isBlank(name)) return null;
		  return map.get(name);
	  }
	}
	
}
